import api from '../../http/apis/apis';
export default {
    namespaced: true,
    state: {
        products: [],
        _id: "",
        myMessage: [],
        productsOne: [],
        theYear: "",
        myOrder: []
    },
    mutations: {
        //设置获取的产品
        setProducts(state, newData) {
            state.products = newData;
        },
        setProductsOne(state, newData) {
            state.productsOne = newData;
        },
        //传入产品的id
        setId(state, _id) {
            state._id = _id;
        },
        //设置个人信息
        setMessage(state, newData) {
            state.myMessage = newData;
            // console.log(state.myMessage);
        },
        //获取今天的年份
        setTheYear(state) {
            let date = new Date();
            state.theYear = date.getFullYear();
        },
        //设置我的订单
        setMyOrder(state, newData) {
            state.myOrder = newData;
            // console.log(state.myOrder);
        }
    },
    actions: {
        //获取当前用户信息
        async getMessage(context, _id) {
            const { data } = await api.homePage.getMessage({ _id });
            // console.log(data.data);
            context.commit('setMessage', data.data);
        },
        //获取产品信息
        async getProducts(context) {
            const { data } = await api.homePage.getProducts();
            console.log(data);
            context.commit('setProducts', data.data);
        },
        //获取产品详情
        async getProductsInfo(context, _id) {
            const { data } = await api.homePage.getProductsInfo({ _id });
            context.commit('setProductsOne', data.data);
        },
        //提交订单
        async createOrder(context) {
            const { data } = await api.homePage.createOrder({
                customerId: context.state.myMessage[0]._id,
                customerName: context.state.myMessage[0].username,
                orderName: context.state.productsOne[0].productName,
                price: context.state.productsOne[0].price,
                ontime: context.state.productsOne[0].ontime,
                useful: 0,
                productsId: context.state.productsId[0]._id,
                productInfo: context.state.productsOne[0].productInfo,
                salesmanId: context.state.myMessage[0].salesmanId
            });
            return data;
        },
        //修改个人信息
        async updateMsg(context, message) {
            const date = new Date();
            let years = date.getFullYear();
            const born = message[0].birth.slice(0, 4);
            console.log(message[0].birth);
            const { data } = await api.homePage.updateMsg({
                _id: message[0]._id,
                username: message[0].username,
                nickname: message[0].nickname,
                birth: message[0].birth,
                address: message[0].address,
                password: message[0].password,
                age: years - born
            });
            console.log(data);
            return data;

        },
        //获取我的订单
        async getMyOrder(context, _id) {
            const { data } = await api.homePage.getMyOrder({ _id });
            context.commit('setMyOrder', data.data);
        },
        //充值
        async updateWallet(context, info) {
            // console.log(info);
            const { data } = await api.homePage.updateWallet({
                money: info.money,
                _id: info._id
            });
            // console.log(data);
            return data;
        },
        //提交理赔信息
        async createCompensation(context, info) {
            // console.log(info);
            const { data } = await api.homePage.createCompensation({
                applyPerson: info.applyPerson,
                applyOrderId: info.applyOrderId,
                applyPrice: info.applyPrice,
                buyTime: info.buyTime,
                img: info.img,
                reason: info.reason,
                salesmanId: info.salesmanId,
                stopTime: info.stopTime
            });
            return data;
        }
    }
}